package cn.wolfcode.luowowo.job;

import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * redis数据持久化任务
 */
@Component
public class RedisDataPersistenceJob {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     *
     * Cron表达式是一个字符串，字符串以5或6个空格隔开，分为6或7个域，每一个域代表一个含义，Cron有如下两种语法格式：
     *　（1）Seconds Minutes Hours DayofMonth Month DayofWeek Year
     *         秒      分     时       几号      月     周几      年
     *　（2）Seconds Minutes Hours DayofMonth Month DayofWeek
     *         秒      分     时       几号      月     周几        spring支持这种
     *         0       0      2        1       *       ？      *  表示在每月的1日的凌晨2点调整业务
     *         0       15     10       ?       *    MON-FRI       表示周一到周五每天上午10:15执行作业
     *
     * @Scheduled:定时任务标签
     * cron：定时任务执行计划
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void doWork() {
        List<Strategy> list = strategyService.listAll();
        for (Strategy s : list) {
            String viewKey = RedisKeys.STRATEGY_VIEWNUM.join(s.getId());
            String replyKey = RedisKeys.STRATEGY_REPLYNUM.join(s.getId());
            String favorKey = RedisKeys.STRATEGY_FAVORNUM.join(s.getId());
            String shareKey = RedisKeys.STRATEGY_SHARENUM.join(s.getId());
            String thumbKey = RedisKeys.STRATEGY_THUMBSUPNUM.join(s.getId());

            s.setViewnum(Integer.valueOf(redisTemplate.opsForValue().get(viewKey)));
            s.setReplynum(Integer.valueOf(redisTemplate.opsForValue().get(replyKey)));
            s.setFavornum(Integer.valueOf(redisTemplate.opsForValue().get(favorKey)));
            s.setSharenum(Integer.valueOf(redisTemplate.opsForValue().get(shareKey)));
            s.setThumbsupnum(Integer.valueOf(redisTemplate.opsForValue().get(thumbKey)));
            System.out.println(redisTemplate.opsForValue().get(viewKey));
            strategyService.updateVO(s);
        }
        List<UserInfo> users = userInfoService.listAll();
        for (UserInfo u : users) {
            String sidskey = RedisKeys.USER_SIDS.join(u.getId());
            String tidskey = RedisKeys.USER_TIDS.join(u.getId());
            u.setSids(JSON.parseArray(redisTemplate.opsForValue().get(sidskey), String.class));
            u.setTids(JSON.parseArray(redisTemplate.opsForValue().get(tidskey), String.class));
            userInfoService.update(u);
        }
        List<Travel> travels = travelService.listAll();
        for (Travel t : travels) {
            String viewkey = RedisKeys.TRAVEL_VIEWNUM.join(t.getId());
            String replykey = RedisKeys.TRAVEL_REPLYNUM.join(t.getId());
            String favorkey = RedisKeys.TRAVEL_FAVORNUM.join(t.getId());
            String sharekey = RedisKeys.TRAVEL_SHARENUM.join(t.getId());
            String thumbkey = RedisKeys.TRAVEL_THUMBSUPNUM.join(t.getId());
            t.setViewnum(Integer.valueOf(redisTemplate.opsForValue().get(viewkey)));
            t.setReplynum(Integer.valueOf(redisTemplate.opsForValue().get(replykey)));
            t.setFavornum(Integer.valueOf(redisTemplate.opsForValue().get(favorkey)));
            t.setSharenum(Integer.valueOf(redisTemplate.opsForValue().get(sharekey)));
            t.setThumbsupnum(Integer.valueOf(redisTemplate.opsForValue().get(thumbkey)));
            travelService.updateVO(t);
        }
        List<UserInfo> userInfos = userInfoService.listAll();
        for (UserInfo user : userInfos) {
            String ttvKey = RedisKeys.USER_TOTALVIEW.join(user.getId());
            String totalView = redisTemplate.opsForValue().get(ttvKey);
            String uidKey = RedisKeys.USER_UIDS.join(user.getId());
            String uidStr = redisTemplate.opsForValue().get(uidKey);
            List<String> uids = JSON.parseArray(uidStr, String.class);
            user.setTotalView(Integer.valueOf(totalView));
            user.setUids(uids);
            userInfoService.update(user);
        }
    }
}
